use std::collections::HashMap;

pub fn t1(n: u16) -> u128 {
    let s = (1..=n as u128).product();
    s
}

pub fn median(numbers: &mut Vec<i32>) -> f32 {
    // TODO: Implement logic here to return the median of the list
    //println!("{:?}", numbers);
    numbers.sort();
    //numbers.sort_by(|a, b| b.cmp(a));
    //println!("{:?}", numbers);
    let len = numbers.len();
    //println!("{:?}", len);
    if len % 2 == 0 {
        println!("{:?}", numbers[len / 2]);
        println!("{:?}", numbers[len / 2 - 1]);
        // idx start at 0
        (numbers[len / 2 - 1] as f32 + numbers[len / 2] as f32) / 2_f32
    } else {
        numbers[(len - 1) / 2] as f32
    }
    //0_f32
}

pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {
    ///* -- 0
    let mut hm: HashMap<i32, i32> = HashMap::new();
    for &n in numbers {
        let t = hm.entry(n).or_insert(0);
        *t += 1;
    }
    let max = (&hm).values().max().unwrap();
    println!("{:?}", max);
    let r: Vec<(&i32, &i32)> = (&hm).into_iter().filter(|(_k, v)| *v == max).collect();
    let mut r: Vec<i32> = r.into_iter().map(|(k, _v)| *k).collect();
    r.sort();
    r
    //*/

    /* --other 1
    // TODO: Implement logic here to return the mode of the list
    let mut hm: HashMap<i32, i32> = HashMap::new();
    for &n in numbers {
        let t = hm.entry(n).or_insert(0);
        *t += 1;
    }
    let max = hm.clone().into_values().max().unwrap();
    let r: Vec<(i32, i32)> = hm.into_iter().filter(|(_k,v)| *v == max).collect();
    let mut r: Vec<i32> = r.into_iter().map(|(k, _v)| k).collect();
    r.sort();
    r
    */

    /* --other 2
    // TODO: Implement logic here to return the mode of the list
    let mut entries = HashMap::new();
    for &number in numbers {
        entries.entry(number).and_modify(|counter| *counter += 1).or_insert(1);
    }
    let max = entries.values().max();
    let mut res = Vec::new();
    for (key, val) in entries.iter() { // entries.iter() 返回类型 iter-item<(&i32, &i32)>
        if Some(val) == max {
            res.push(*key)
        }
    }
    res.sort();
    res
    */

    /* -- other 4
    let mut freq: HashMap<i32, i32> = HashMap::new();
    for val in numbers {
        freq.entry(*val).and_modify(|counter| *counter += 1).or_insert(1);
    };
    let highest_count = freq.values().max().unwrap(); // highest_count 是一个引用
    let mut highest_freq: Vec<i32> = Vec::new();
    for (k, v) in &freq { // (k, v) 的类型是 (&i32, &i32)
        if v == highest_count {
            highest_freq.push(*k);
        }
    }
    highest_freq.sort();
    highest_freq
    */

    /* -- 5
    let mut hash = HashMap::new();
    for n in numbers {
        hash.entry(n).and_modify(|number| *number += 1).or_insert(1);
        /*
        match hash.get(n) {
            Some(&number) => { hash.insert(n, number + 1); },
            _ => { hash.insert(n, 1); }
        }
        */
    }
    let mut r: Vec<i32> = vec![];
    let mut mode_freq = 0;
    for (k, v) in hash.into_iter() {
        if v > mode_freq {
            if r.len() > 0 {
                r.clear();
            }
            mode_freq = v;
            r.push(*k);
        } else if v == mode_freq {
            r.push(*k);
        }
    }
    r.sort();
    r
    */
}
